package com.ittest.controller;
import com.ittest.server.RedisDynamicKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/api")
public class RedisDynamicKeyController {

    @Autowired
    private RedisDynamicKeyService deleteByDynamicParams;

    /**
     * 删除 Redis 中符合动态参数的 key
     * @param pagecode 动态页面标识（如 2025_pre_hotel）
     * @param activitycode 动态活动标识（如 2025_pre_hotel_bargain）
     * @param username 动态用户名（如 qjtpwmi0273）
     * @return 删除结果
     */
    @GetMapping("/delredis")
    public Map<String, Object> deleteDynamicKey(
            @RequestParam String pagecode,
            @RequestParam String activitycode,
            @RequestParam String username) {
        Map<String, Object> result = new HashMap<>();
        try {
            int deletedCount = deleteByDynamicParams.deleteByDynamicParams(pagecode, activitycode, username);
            result.put("success", true);
            result.put("message", "删除成功");
            result.put("deletedCount", deletedCount);
            result.put("deletedKey", deleteByDynamicParams.buildKey(pagecode, activitycode, username)); // 返回被删除的 key
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
            result.put("deletedCount", 0);
        }
        return result;
    }
}